
/* bitDemo.c
  * Author:
 *      sibuser
  */

/*
 * Program prints value and shows bitwise operators
 */

#include <stdio.h>
typedef unsigned int uint;

/* returns the a_Number bits in positions a_Start in a_Value, right-adjusted */
uint GetBit(uint a_Value, uint a_Start, uint a_Number)
{
    return ((a_Value >> (a_Start+1-a_Number)) & ~(~0 << a_Number));
}

/* Prints a_Value with the a_Number bits that begin at position
 * a_Start set to the rightmost  bits of a_Bit, leaving the other bits unchanged.
 */
void SetBit(uint a_Value, int a_Start, int a_Number, uint a_Bit)
{
    const uint size = 8;// sizeof(int)*8;
    uint bits[size];
    int i;
    for (i = 0; i < size; ++i){
         if (i > 3 &&  i <= 5 ){
            if (a_Bit == 1){
                bits[i] = (a_Value >> i) & ~0x1;
                continue;
            } else {
                bits[i] = (a_Value >> i) & 0x0;
                continue;
            }
           }bits[i] = (a_Value >> i) & 0x1;
    }
    for (i = size - 1; i >= 0; --i){
        printf("%d ", bits[i]);
    }
    printf("\n");
}
/* 
 * bref      Printing a number in binary format 
 * param [in] a_Value           Value for print
*/

void PrintBits(uint a_Value)
 {
     const uint size = 8;//sizeof(int)*8;
    uint bit[size];
    int i;
    for (i = 0; i < size; ++i){
        bit[i] = (a_Value >> i) & 0x1;
    }
    for (i = size - 1; i >= 0; --i){
        printf("%d ", bit[i]);
    }
    printf("\n");

 }

int main(int argc, const char** argv)
{
    uint value = 0;
    /*    uint position = 0;
          uint numberBit = 0;*/
    
    printf("Enter digit: ");
    scanf("%d", &value);
    
    printf("\nOriginal value\n");
    PrintBits(value);
        
    printf("\nValue is valid if bits in mask is active\n");
    PrintBits((value ^ (1 << 1 | 1 << 4 | 1 << 6)) & ~(1 << 1 | 1 << 4 | 1 << 6));
    if (!((value ^ (1 << 1 | 1 << 4 | 1 << 6)) & ~(1 << 1 | 1 << 4 | 1 << 6))){
        printf("Input number is valid\n");
    }
    else printf("Input number is not valid\n");

    printf("\nValue is valid if mask is valid \n");
    PrintBits(value | (1 << 1 | 1 << 4 | 1 << 6));
    if (!(value ^ (1 << 1 | 1 << 4 | 1 << 6))){
        printf("Input number is valid\n");
    }
    else printf("Input number is not valid\n");

    printf("\nAny bits active and mask is valid\n");
    PrintBits(value & (1 << 1 | 1 << 4 | 1 << 6));
    if (!((value & (1 << 1 | 1 << 4 | 1 << 6)) ^ (1 << 1 | 1 << 4 | 1 << 6))){
        printf("Input number is valid\n");
    }
    else printf("Input number is not valid\n");
     
    return 0;
}


